﻿package com.waitingsnow.core.display.control
{
	
	import flash.accessibility.AccessibilityProperties;
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.*;
	import flash.utils.Dictionary;
	
	/**
	 * tootip显示类
	 * @author chengang
	 * 
	 */	
	public class ToolTip_test extends Sprite	
	{
		private static var instance:ToolTip_test = null;
		private var label:TextField;		
		private var area:DisplayObject;
		
		//tip数据字典
		private static var tipDict:Dictionary = new Dictionary();
		
		//背景色	
		private var bgColor:uint = 0xcccccc;
		//背景透明度
		private var bgAlpha:Number = 0.5;
		//背景线框颜色
		private var lineColor:uint = 0x000000;
		
		//tip框类型 0/1/2/3分别代表4种类型框 左上/右上/左下/右下
		private var _type:int = -1;
		// 容器与文本的水平和垂直间隔
		private const vVal:int = 2;
		private const hVal:int = 5;
		//三角形长高
		private const triHVal:int = 6;
		private const triVVal:int = 6;
		/*三角形距离矩形边界*/
		private const leftVal:int = 6;
		private const cicleWidth:int = 5;
		
		public function ToolTip_test() 
		{
			label = new TextField();
			label.autoSize = TextFieldAutoSize.LEFT;
			label.selectable = false;
			/*label.multiline = true;*/
			label.defaultTextFormat = new TextFormat("宋体", 12, 0x666666);
			label.htmlText = "提示信息";
			addChild(label);
			visible = false;
			mouseEnabled = mouseChildren = false;
		}
		
		
		/**
		 * 显示TIP信息  如果有注册过则只更改文本信息
		 * 如果显示对象被禁用，则添加新影片，删除时新影片删除
		 * @param area
		 * @param message
		 * 
		 */		
		public static function show(area:InteractiveObject, message:String, tipWidth:Number = 0, bgColor:uint = 0x666666, lineColor:uint = 0x333333, txtColor:uint = 0xffffff, bgAlpha:Number = 0.8):void 
		{
			if(tipDict[area] && tipDict[area]['label'])
			{
				tipDict[area]['label'].htmlText = message;
				return;
			}
			
			instance = new ToolTip_test();
			tipDict[area] = {};
			tipDict[area]['mc'] = area;
			tipDict[area]['label'] = instance.label;
			area.parent.addChild(instance);
			
			if(!area.mouseEnabled)
			{
				var sp:Sprite = new Sprite();
				var rect:Rectangle = area.getBounds(area.parent);
				sp.graphics.clear();
				sp.graphics.beginFill(0x0, 0);
				sp.graphics.drawRect(0, 0, rect.width, rect.height);
				sp.graphics.endFill();
				sp.x = rect.x;
				sp.y = rect.y;
				
				area.parent.addChild(sp);
				area = sp;
			}
			
			var prop:AccessibilityProperties = new AccessibilityProperties();
			prop.description = message;
			area.accessibilityProperties = prop;
			area.addEventListener(MouseEvent.MOUSE_OVER, instance.handler);
			instance.area = area;
			//赋初始值
			if(tipWidth != 0)
			{
				instance.label.wordWrap = true;
				instance.label.width = tipWidth;
			}
			instance.label.textColor = txtColor;
			instance.bgAlpha = bgAlpha;
			instance.bgColor = bgColor;
			instance.lineColor = lineColor;
		}
		
		/**
		 * 移除tip  移除新增加的影片，移除tip影片
		 * @param area
		 * 
		 */		
		public static function removeTip(area:InteractiveObject):void 
		{
			if(tipDict[area] && instance)
			{
				if(!area.mouseEnabled)
				{
					area.parent.removeChild(instance.area);
					instance.area.removeEventListener(MouseEvent.MOUSE_OVER, instance.handler);
					instance.area = null;
				}
				else
					area.removeEventListener(MouseEvent.MOUSE_OVER, instance.handler);
				area.parent.removeChild(instance);
				tipDict[area] = null;
			}
		}
		
		/**
		 * 显示tip
		 * @param area
		 * 
		 */		
		private function showTip(area:DisplayObject):void 
		{
			area.addEventListener(MouseEvent.MOUSE_OUT, this.handler);
			area.addEventListener(MouseEvent.MOUSE_MOVE, this.handler);
			label.htmlText = area.accessibilityProperties.description;
			if(tipDict[area])
				visible = true;
			else	
				redraw();			
		}
		
		/**
		 * 隐藏tip
		 * 
		 */		
		private function hideTip():void	
		{
			this.area.removeEventListener(MouseEvent.MOUSE_OUT, this.handler);
			this.area.removeEventListener(MouseEvent.MOUSE_MOVE, this.handler);
			this.area = null;
			visible = false;
		}
		
		/**
		 * 移动
		 * @param point
		 * 
		 */		
		private function move(point:Point):void 
		{			 
			var lp:Point = this.parent.globalToLocal(point);
			this.x = lp.x;			
			this.y = lp.y;
			
			if(!visible)
				visible = true;
		}
		
		/**
		 * 事件处理
		 * @param event
		 * 
		 */		
		private function handler(event:MouseEvent):void	{
			switch(event.type) 
			{
				case MouseEvent.MOUSE_OUT:
					this.hideTip();
					break;
				case MouseEvent.MOUSE_MOVE:
					this.move(new Point(event.stageX, event.stageY));	
					redraw();
					break;
				case MouseEvent.MOUSE_OVER:
					this.area = event.target as DisplayObject;
					this.move(new Point(event.stageX, event.stageY));
					this.showTip(this.area);
					break;
			}
		}
		
		/**
		 * 判断类型
		 * @param x 鼠标的x/y坐标
		 * @param y
		 * 
		 */		
		private function checkType(x:Number, y:Number):Boolean
		{
			//是否左边标签
			var leftFlag:Boolean = (x + this.label.width + 2*hVal>= area.parent.width)?false:true;
			var _oldType:int = _type;
			
			//三角形在上边
			if(y <= this.label.height + 2*vVal + this.triVVal)
			{
				if(leftFlag)
					_type = 0;
				else
					_type = 1;
			}
				//下边
			else
			{
				if(leftFlag)
					_type = 2;
				else
					_type = 3;
			}
			
			return (_oldType == _type)?false:true;
		}
		
		/**
		 * 重绘矩形框
		 * @return 
		 * 
		 */		
		private function redraw() :void
		{
			//判断类型  无变化不重绘
			if(!checkType(this.x, this.y))
				return;
			
			this.graphics.clear();
			this.graphics.beginFill(this.bgColor, this.bgAlpha);
			this.graphics.lineStyle(0, this.lineColor, this.bgAlpha);
			this.graphics.moveTo(0, 0);
			
			switch(_type)
			{
				case 0:
					this.graphics.lineTo(-triHVal*0.5, triVVal);
					this.graphics.lineTo(-triHVal*0.5 - this.leftVal, triVVal);
					this.graphics.lineTo(-triHVal*0.5 - this.leftVal, triVVal + label.height + 2*this.vVal);
					this.graphics.lineTo(-triHVal*0.5 - this.leftVal + label.width + 2*this.hVal, triVVal + label.height + 2*this.vVal);
					this.graphics.lineTo(-triHVal*0.5 - this.leftVal + label.width + 2*this.hVal, triVVal);
					this.graphics.lineTo(triHVal*0.5, triVVal);
					
					label.x = -triHVal*0.5 - this.leftVal + this.hVal;
					label.y = triVVal + this.vVal;
					break;
				case 1:
					this.graphics.lineTo(-triHVal*0.5, triVVal);
					this.graphics.lineTo(triHVal*0.5 + this.leftVal - label.width - 2*this.hVal, triVVal);
					this.graphics.lineTo(triHVal*0.5 + this.leftVal - label.width - 2*this.hVal, triVVal + label.height + 2*this.vVal);
					this.graphics.lineTo(triHVal*0.5 + this.leftVal, triVVal + label.height + 2*this.vVal);
					this.graphics.lineTo(triHVal*0.5 + this.leftVal, triVVal);
					this.graphics.lineTo(triHVal*0.5, triVVal);
					
					label.x = triHVal*0.5 + this.leftVal - label.width - this.hVal;
					label.y = triVVal + this.vVal;
					break;
				case 2:
					this.graphics.lineTo(-triHVal*0.5, -triVVal);
					this.graphics.lineTo(-triHVal*0.5 - this.leftVal, -triVVal);
					this.graphics.lineTo(-triHVal*0.5 - this.leftVal, -triVVal - label.height - 2*this.vVal);
					this.graphics.lineTo(-triHVal*0.5 - this.leftVal + label.width + 2*this.hVal, -triVVal - label.height - 2*this.vVal);
					this.graphics.lineTo(-triHVal*0.5 - this.leftVal + label.width + 2*this.hVal, -triVVal);
					this.graphics.lineTo(triHVal*0.5, -triVVal);
					
					label.x = -triHVal*0.5 - this.leftVal + this.hVal;
					label.y = -triVVal - label.height - this.vVal;
					break;
				case 3:
					this.graphics.lineTo(-triHVal*0.5, -triVVal);
					this.graphics.lineTo(triHVal*0.5 + this.leftVal - label.width - 2*this.hVal, -triVVal);
					this.graphics.lineTo(triHVal*0.5 + this.leftVal - label.width - 2*this.hVal, -triVVal - label.height - 2*this.vVal);
					this.graphics.lineTo(triHVal*0.5 + this.leftVal, -triVVal +- label.height - 2*this.vVal);
					this.graphics.lineTo(triHVal*0.5 + this.leftVal, -triVVal);
					this.graphics.lineTo(triHVal*0.5, -triVVal);
					
					label.x = triHVal*0.5 + this.leftVal - label.width - this.hVal;
					label.y = -triVVal - label.height - this.vVal;
					break;
			}
			this.graphics.lineTo(0, 0);
			this.graphics.endFill();
		}
		
		
	}
}